/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.csm.sim2k11.frontend;

import java.awt.BasicStroke;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author Santiago Moreno
 */
public class StadisticsWindow extends javax.swing.JDialog {

    private XYSeries srClients, srClientsLost;

    /**
     * Creates new form StadisticsWindow
     */
    public StadisticsWindow( ArrayList<int[]> historicSimulations) {
        this.setIconImage(new ImageIcon(getClass().getResource("/icons/16/chart_bar.png")).getImage());
        initComponents();
        srClients = new XYSeries("Clientes Atendidos");
        srClientsLost = new XYSeries("Clientes Perdidos");
        int sumClients, sumLostClients;
        sumClients = sumLostClients = 0;
        for (int i = 0; i < historicSimulations.size(); i++) {
            srClients.add(i, historicSimulations.get(i)[0]);
            srClientsLost.add(i, historicSimulations.get(i)[1]);
            sumClients += historicSimulations.get(i)[0];
            sumLostClients += historicSimulations.get(i)[1];
        }
        lblCantSimulations.setText(""+historicSimulations.size());
        lblAtendedClients.setText(""+(int)(sumClients/historicSimulations.size()));
        lblLostClients.setText(""+(int)(sumLostClients/historicSimulations.size()));
        updateStadistics();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblCurve = new javax.swing.JLabel();
        pnlInvariants = new javax.swing.JPanel();
        lblTitleAtendedClients = new javax.swing.JLabel();
        lblTitleLostClients = new javax.swing.JLabel();
        lblTitleSimulationQuanitity = new javax.swing.JLabel();
        lblCantSimulations = new javax.swing.JLabel();
        lblAtendedClients = new javax.swing.JLabel();
        lblLostClients = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Estadísticas");
        setIconImage(null);
        setModal(true);
        setResizable(false);

        lblCurve.setText("[chart]");

        pnlInvariants.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos:"));

        lblTitleAtendedClients.setText("Prom. de Clientes Atendidos:");

        lblTitleLostClients.setText("Prom. de Clientes Perdidos:");

        lblTitleSimulationQuanitity.setText("Cantidad de Simulaciones:");

        lblCantSimulations.setFont(new java.awt.Font("Ubuntu", 1, 18));
        lblCantSimulations.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblCantSimulations.setText("jLabel4");

        lblAtendedClients.setFont(new java.awt.Font("Ubuntu", 1, 18));
        lblAtendedClients.setForeground(new java.awt.Color(40, 68, 228));
        lblAtendedClients.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblAtendedClients.setText("jLabel5");

        lblLostClients.setFont(new java.awt.Font("Ubuntu", 1, 18));
        lblLostClients.setForeground(new java.awt.Color(217, 30, 30));
        lblLostClients.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblLostClients.setText("jLabel6");

        javax.swing.GroupLayout pnlInvariantsLayout = new javax.swing.GroupLayout(pnlInvariants);
        pnlInvariants.setLayout(pnlInvariantsLayout);
        pnlInvariantsLayout.setHorizontalGroup(
            pnlInvariantsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlInvariantsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlInvariantsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(lblCantSimulations, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblTitleSimulationQuanitity, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(29, 29, 29)
                .addGroup(pnlInvariantsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblAtendedClients, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblTitleAtendedClients, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addGroup(pnlInvariantsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblLostClients, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblTitleLostClients, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnlInvariantsLayout.setVerticalGroup(
            pnlInvariantsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlInvariantsLayout.createSequentialGroup()
                .addGroup(pnlInvariantsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTitleLostClients)
                    .addComponent(lblTitleSimulationQuanitity)
                    .addComponent(lblTitleAtendedClients))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlInvariantsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCantSimulations)
                    .addComponent(lblAtendedClients)
                    .addComponent(lblLostClients))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblCurve, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 672, Short.MAX_VALUE)
                    .addComponent(pnlInvariants, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(lblCurve, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlInvariants, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-696)/2, (screenSize.height-477)/2, 696, 477);
    }// </editor-fold>//GEN-END:initComponents

    private void updateStadistics() {

        XYSeriesCollection xyData = new XYSeriesCollection();
        xyData.addSeries(srClientsLost);
        xyData.addSeries(srClients);
        

        JFreeChart chart = ChartFactory.createXYLineChart(
                "",
                "Número de Simulación",
                "Cant. Clientes",
                xyData,
                PlotOrientation.VERTICAL,
                true,
                false,
                false);
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.getRenderer().setStroke(new BasicStroke(2));
        BufferedImage image = chart.createBufferedImage(650, 320);

        lblCurve.setIcon(new ImageIcon(image));
        lblCurve.setText("");
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lblAtendedClients;
    private javax.swing.JLabel lblCantSimulations;
    private javax.swing.JLabel lblCurve;
    private javax.swing.JLabel lblLostClients;
    private javax.swing.JLabel lblTitleAtendedClients;
    private javax.swing.JLabel lblTitleLostClients;
    private javax.swing.JLabel lblTitleSimulationQuanitity;
    private javax.swing.JPanel pnlInvariants;
    // End of variables declaration//GEN-END:variables
}
